﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using log4net;
using System.IO;
using DWH.Utils;
using DWH.Data;
using System.Drawing;

namespace DWH.PP.LoopUrls.tumblr
{
    public class TumblrPicturePullTask : PPLoopUrlsBoardPullTask
    {
        private string tumblrSiteName = "unknown";
        private string thisAttachmentStoreFolder;

        private ILog logger;

        public override void Initialize()
        {
            base.Initialize();

            logger = LogManager.GetLogger(typeof(TumblrPicturePullTask));
            logger.InfoFormat("tumblr task start {0}", TaskName);



            if (ExtendData.ContainsKey(DWHConstants.KEY_TUMBLR_SITE_NAME))
            {
                tumblrSiteName = ExtendData[DWHConstants.KEY_TUMBLR_SITE_NAME];
            }

            thisAttachmentStoreFolder = Path.Combine(attachmentStoreFolder, tumblrSiteName);
            DWHHelper.CreateWebAttachmentFolderIfNotExists(thisAttachmentStoreFolder);

            logger.InfoFormat("start to download pictures from tumblr site:{0}", tumblrSiteName);
        }

        public override void ProcessPageContent(string pageUrl)
        {
            //DWHHelper.DownloadWebPageImage(TaskName, thisAttachmentStoreFolder, pageUrl);
            //if (!DWHDBHelper.IsPageVisited(pageUrl.GetHashCode()))
            //{
            //    DWHHelper.DownloadWebPageImage(TaskName, thisAttachmentStoreFolder, pageUrl);
            //}

            if (DWHDBHelper.IsPageVisited(pageUrl.GetHashCode()) || DWHDBHelper.IsPageAttachmentVisited(pageUrl.GetHashCode()))
            {
                logger.InfoFormat("this image has been proceed:{0}",pageUrl);
            }
            else
            {

                logger.InfoFormat("processing tumblr image from site:{0}, imageurl:{1}", tumblrSiteName, pageUrl);

                if (!String.IsNullOrEmpty(currentProcessingPageListUrl))
                {
                    string subPath = Path.Combine(thisAttachmentStoreFolder, currentProcessingPageListUrl.GetHashCode().ToString());

                    DWHDBHelper.AddPage(TaskName, pageUrl.GetHashCode(), pageUrl, tumblrSiteName, subPath, DateTime.Now);

                    DWHDBHelper.AddUndownloadedPageAttachment(pageUrl, pageUrl.GetHashCode(),
                        DWHConstants.ATTACHMENT_TYPE_IMAGE,
                        subPath,
                        DateTime.Now);
                }
                else
                {
                    DWHDBHelper.AddPage(TaskName, pageUrl.GetHashCode(), pageUrl, tumblrSiteName, thisAttachmentStoreFolder, DateTime.Now);

                    DWHDBHelper.AddUndownloadedPageAttachment(pageUrl, pageUrl.GetHashCode(),
                            DWHConstants.ATTACHMENT_TYPE_IMAGE,
                            thisAttachmentStoreFolder, DateTime.Now);
                }
            }
        }
    }
}
